package com.weiyi.weiman.quartz;

import com.alibaba.fastjson.JSON;
import com.weiyi.weiman.domain.Const.ComicsConst;
import com.weiyi.weiman.mapper.comics.ComicsMapper;
import com.weiyi.weiman.utils.RedisUtils;
import comics.Comics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @Author zhang
 * @Date 2021/5/16 下午3:50
 * @Description 修改漫画信息的定时控制类
 * @Version 1.0
 **/
@Component
@Slf4j
public class ComicsTask {
    @Autowired
    private ComicsMapper comicsMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 更新漫画信息
     */
    @Scheduled(cron = "0 0 1 * * ?")
    private void updateComics() {
        Set<String> allKeys = redisUtils.getAllKeys(ComicsConst.COMICS_REDIS_PRE + "*");
        for (String key : allKeys) {
            Map<Object, Object> comicsJson = redisUtils.hGetAll(key);
            Comics comics = JSON.parseObject(JSON.toJSONString(comicsJson), Comics.class);
            comicsMapper.updateById(comics);
            log.info("漫画:{} 信息更新成功", comics.getComicsId());
        }
    }

    /**
     * 更新漫画排名信息
     */
    @Scheduled(cron = "0 0 */12 * * ?")
    private void updateComicsRank() {
        //更新人气榜单

    }
}